home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / asymptopia / Tux.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-16  |  14.7 KB  |  598 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. """
  5. /***************************************************************************
  6.  
  7. \tAuthor \t\t\t:Charles B. Cosse 
  8. \t
  9. \tEmail\t\t\t:ccosse@asymptopia.com
  10. \t\t\t\t\t
  11. \t\t\t\t\t
  12. \tCopyright\t\t:(C) 2002,2003 Asymptopia Software.
  13. \t
  14.  ***************************************************************************/
  15. /***************************************************************************
  16.                           Tux.py
  17.  
  18. \tDescription: The Tux object generates & submits Tux's options.
  19.  
  20.  ***************************************************************************/
  21.  
  22. /***************************************************************************
  23.  *                                                                         *
  24.  *   This program is free software; you can redistribute it and/or modify  *
  25.  *   it under the terms of the GNU General Public License as published by  *
  26.  *   the Free Software Foundation; either version 2 of the License, or     *
  27.  *   (at your option) any later version. (Please note that if you use this *
  28.  *   code you must give credit by including the Author and Copyright       *
  29.  *   info at the top of this file).                                        *
  30.  ***************************************************************************/
  31. """
  32. import os
  33. import pygame
  34. from pygame.locals import *
  35. from random import random
  36. from Board import *
  37. from Spot import *
  38. from myutil import *
  39. from Tile import *
  40. from Button import *
  41. from Validator import *
  42. from Player import Player
  43.  
  44. class Tux(Player):
  45.     '''Tux derives from asymptopia.Player
  46. \tWe could have called it "penguin manager"
  47. \tor "Dealer", as in v.01.
  48. \tTux just get instantiated with a ptr->TuxMathScrabble, itself.
  49. \t
  50. \tNeed to add/take-off from submissionspots from here.
  51. \tWant to parallel same interface as player does, but can\'t use same
  52. \tactual lines of code, since Tux knows what he\'s doing and the board
  53. \thas yet to validate Tux\'s submission.
  54. \t
  55. \tTux takes care of his own tray.
  56. \tHe uses same "draw_tiles" func that gets called from play() after 
  57. \twin sound, upon successful validation and subsequent guest-exchange between
  58. \tlayers (submission vs board).
  59. \t'''
  60.     
  61.     def __init__(self, game):
  62.         Player.__init__(self)
  63.         self.game = game
  64.         self.tray = game.tuxtray
  65.         self.LEVEL = game.LEVEL
  66.         self.current_submission_idx = None
  67.         self.triplets = None
  68.         self.doublets = None
  69.         self.singlets = None
  70.         self.operator_doublets = None
  71.         self.operator_singlets = None
  72.         self.triplet_expressions = None
  73.         self.doublet_expressions = None
  74.         self.singlet_expressions = None
  75.         self.wc_doublet_expressions = None
  76.         self.wc_singlet_expressions = None
  77.  
  78.     
  79.     def getStringValues(self):
  80.         str_values = []
  81.         spots = self.tray.get_spots()
  82.         nnumbers = self.game.NNUMBERS
  83.         newspots = []
  84.         for spotidx in range(nnumbers):
  85.             newspots.append(0)
  86.         
  87.         ntrayspots = self.game.NTRAYSPOTS
  88.         while len(spots) > ntrayspots - nnumbers:
  89.             for spot in spots:
  90.                 if spot.getMN()[1] < nnumbers:
  91.                     newspots[spot.getMN()[1]] = spot
  92.                     spots.remove(spot)
  93.                 
  94.             
  95.         for dummy in range(len(newspots)):
  96.             str_values.append(newspots[dummy].guest.str_val)
  97.         
  98.         return str_values
  99.  
  100.     
  101.     def getAllStringValues(self):
  102.         str_values = []
  103.         spots = self.tray.get_spots()
  104.         ntrayspots = self.game.NTRAYSPOTS
  105.         newspots = []
  106.         for spotidx in range(ntrayspots):
  107.             newspots.append(0)
  108.         
  109.         while len(spots) > 0:
  110.             for spot in spots:
  111.                 newspots[spot.getMN()[1]] = spot
  112.                 spots.remove(spot)
  113.             
  114.         for dummy in range(len(newspots)):
  115.             str_values.append(newspots[dummy].guest.str_val)
  116.         
  117.         return str_values
  118.  
  119.     
  120.     def cycle_vals(self, vals):
  121.         tmp = vals.pop()
  122.         vals.insert(int(random() * len(vals)), tmp)
  123.         return vals
  124.  
  125.     
  126.     def get3x2x1x(self, N):
  127.         plets = []
  128.         num_times_cycled = 0
  129.         str_vals = self.getStringValues()
  130.         NumNumbers = 6
  131.         while num_times_cycled < 1000:
  132.             for idx in range(0, NumNumbers - N):
  133.                 i_plet = []
  134.                 s_plet = []
  135.                 for jdx in range(N):
  136.                     i_plet.append(float(str_vals[idx + jdx]))
  137.                 
  138.                 i_plet.sort()
  139.                 for jdx in range(N):
  140.                     s_plet.append(`i_plet[jdx]`)
  141.                 
  142.                 if plets.count(s_plet) == 0:
  143.                     plets.append(s_plet)
  144.                 elif N == 1 and plets.count(s_plet) < str_vals.count(s_plet[0]):
  145.                     plets.append(s_plet)
  146.                 
  147.             
  148.             str_vals = self.cycle_vals(str_vals)
  149.             num_times_cycled = num_times_cycled + 1
  150.         return plets
  151.  
  152.     
  153.     def evaluate(self, expr):
  154.         str = ''
  155.         for idx in range(len(expr)):
  156.             str = str + expr[idx]
  157.         
  158.         
  159.         try:
  160.             val = eval(str)
  161.         except:
  162.             return 0
  163.  
  164.         return val
  165.  
  166.     
  167.     def get3xPermutations(self, tripplet):
  168.         p = [
  169.             [
  170.                 1,
  171.                 2,
  172.                 3],
  173.             [
  174.                 1,
  175.                 3,
  176.                 2],
  177.             [
  178.                 2,
  179.                 1,
  180.                 3],
  181.             [
  182.                 2,
  183.                 3,
  184.                 1],
  185.             [
  186.                 3,
  187.                 1,
  188.                 2],
  189.             [
  190.                 3,
  191.                 2,
  192.                 1]]
  193.         px3 = []
  194.         for pidx in range(len(p)):
  195.             tx3 = []
  196.             for tidx in range(3):
  197.                 tx3.append(tripplet[p[pidx][tidx] - 1])
  198.             
  199.             if px3.count(tx3) == 0:
  200.                 px3.append(tx3)
  201.             
  202.         
  203.         return px3
  204.  
  205.     
  206.     def get2xPermutations(self, doublet):
  207.         p = [
  208.             [
  209.                 1,
  210.                 2],
  211.             [
  212.                 2,
  213.                 1]]
  214.         px2 = []
  215.         for pidx in range(len(p)):
  216.             dx2 = []
  217.             for tidx in range(2):
  218.                 dx2.append(doublet[p[pidx][tidx] - 1])
  219.             
  220.             if px2.count(dx2) == 0:
  221.                 px2.append(dx2)
  222.             
  223.         
  224.         return px2
  225.  
  226.     
  227.     def get_operator_plets(self, N):
  228.         if N == 1:
  229.             plets = [
  230.                 [
  231.                     '+'],
  232.                 [
  233.                     '-'],
  234.                 [
  235.                     '*'],
  236.                 [
  237.                     '/']]
  238.         elif N == 2 and self.LEVEL <= 4:
  239.             plets = [
  240.                 [
  241.                     '+',
  242.                     '+'],
  243.                 [
  244.                     '+',
  245.                     '-'],
  246.                 [
  247.                     '-',
  248.                     '+'],
  249.                 [
  250.                     '-',
  251.                     '-'],
  252.                 [
  253.                     '*',
  254.                     '*'],
  255.                 [
  256.                     '*',
  257.                     '/'],
  258.                 [
  259.                     '/',
  260.                     '*'],
  261.                 [
  262.                     '/',
  263.                     '/'],
  264.                 [
  265.                     '*',
  266.                     '-'],
  267.                 [
  268.                     '-',
  269.                     '*'],
  270.                 [
  271.                     '*',
  272.                     '+'],
  273.                 [
  274.                     '+',
  275.                     '*'],
  276.                 [
  277.                     '/',
  278.                     '-'],
  279.                 [
  280.                     '-',
  281.                     '/'],
  282.                 [
  283.                     '/',
  284.                     '+'],
  285.                 [
  286.                     '+',
  287.                     '/']]
  288.         elif N == 2 and self.LEVEL < 3:
  289.             plets = [
  290.                 [
  291.                     '+',
  292.                     '+'],
  293.                 [
  294.                     '+',
  295.                     '-'],
  296.                 [
  297.                     '-',
  298.                     '+'],
  299.                 [
  300.                     '-',
  301.                     '-']]
  302.         
  303.         return plets
  304.  
  305.     
  306.     def generate_expressions(self):
  307.         triplets = self.get3x2x1x(3)
  308.         doublets = self.get3x2x1x(2)
  309.         singlets = self.get3x2x1x(1)
  310.         operator_doublets = self.get_operator_plets(2)
  311.         operator_singlets = self.get_operator_plets(1)
  312.         current_submission_idx = 0
  313.         triplet_expressions = []
  314.         for pdx in range(0, len(triplets)):
  315.             permutations = self.get3xPermutations(triplets[pdx])
  316.             for perm in permutations:
  317.                 for odx in range(len(operator_doublets)):
  318.                     expr = [
  319.                         perm[0],
  320.                         operator_doublets[odx][0],
  321.                         perm[1],
  322.                         operator_doublets[odx][1],
  323.                         perm[2]]
  324.                     value = self.evaluate(expr)
  325.                     triplet_expressions.append([
  326.                         pdx,
  327.                         odx,
  328.                         expr,
  329.                         value])
  330.                 
  331.             
  332.         
  333.         doublet_expressions = []
  334.         for pdx in range(0, len(doublets)):
  335.             permutations = self.get2xPermutations(doublets[pdx])
  336.             for perm in permutations:
  337.                 for odx in range(len(operator_singlets)):
  338.                     expr = [
  339.                         perm[0],
  340.                         operator_singlets[odx][0],
  341.                         perm[1]]
  342.                     value = self.evaluate(expr)
  343.                     doublet_expressions.append([
  344.                         pdx,
  345.                         odx,
  346.                         expr,
  347.                         value])
  348.                 
  349.             
  350.         
  351.         wc_doublet_expressions = []
  352.         for pdx in range(0, len(singlets)):
  353.             for odx in range(len(operator_singlets)):
  354.                 for val in range(21):
  355.                     expr = [
  356.                         `val` + '.0',
  357.                         operator_singlets[odx][0],
  358.                         singlets[pdx][0]]
  359.                     value = self.evaluate(expr)
  360.                     expr = [
  361.                         'WC:' + `val` + '.0',
  362.                         operator_singlets[odx][0],
  363.                         singlets[pdx][0]]
  364.                     wc_doublet_expressions.append([
  365.                         pdx,
  366.                         odx,
  367.                         expr,
  368.                         value])
  369.                 
  370.             
  371.         
  372.         for pdx in range(0, len(singlets)):
  373.             for odx in range(len(operator_singlets)):
  374.                 for val in range(21):
  375.                     expr = [
  376.                         singlets[pdx][0],
  377.                         operator_singlets[odx][0],
  378.                         `val` + '.0']
  379.                     value = self.evaluate(expr)
  380.                     expr = [
  381.                         singlets[pdx][0],
  382.                         operator_singlets[odx][0],
  383.                         'WC:' + `val` + '.0']
  384.                     wc_doublet_expressions.append([
  385.                         pdx,
  386.                         odx,
  387.                         expr,
  388.                         value])
  389.                 
  390.             
  391.         
  392.         for pdx in range(0, len(doublets)):
  393.             permutations = self.get2xPermutations(doublets[pdx])
  394.             for perm in permutations:
  395.                 for odx in range(len(operator_singlets)):
  396.                     expr = [
  397.                         perm[0],
  398.                         operator_singlets[odx][0],
  399.                         perm[1]]
  400.                     value = self.evaluate(expr)
  401.                     expr = [
  402.                         perm[0],
  403.                         'WC:' + operator_singlets[odx][0],
  404.                         perm[1]]
  405.                     wc_doublet_expressions.append([
  406.                         pdx,
  407.                         odx,
  408.                         expr,
  409.                         value])
  410.                 
  411.             
  412.         
  413.         wc_wc_doublet_expressions = []
  414.         for v1 in range(21):
  415.             for v2 in range(21):
  416.                 for odx in range(len(operator_singlets)):
  417.                     expr = [
  418.                         `v1` + '.0',
  419.                         operator_singlets[odx][0],
  420.                         `v2` + '.0']
  421.                     value = self.evaluate(expr)
  422.                     expr = [
  423.                         'WC:' + `v1` + '.0',
  424.                         operator_singlets[odx][0],
  425.                         'WC:' + `v2` + '.0']
  426.                     wc_wc_doublet_expressions.append([
  427.                         0,
  428.                         odx,
  429.                         expr,
  430.                         value])
  431.                 
  432.             
  433.         
  434.         singlet_expressions = []
  435.         for pdx in range(0, len(singlets)):
  436.             expr = [
  437.                 singlets[pdx][0]]
  438.             value = self.evaluate(expr[0])
  439.             singlet_expressions.append([
  440.                 pdx,
  441.                 None,
  442.                 expr,
  443.                 value])
  444.         
  445.         wc_singlet_expressions = []
  446.         for pdx in range(0, 21):
  447.             expr = [
  448.                 'WC:' + `pdx` + '.0']
  449.             value = pdx
  450.             wc_singlet_expressions.append([
  451.                 pdx,
  452.                 None,
  453.                 expr,
  454.                 value])
  455.         
  456.         self.triplet_expressions = triplet_expressions
  457.         self.doublet_expressions = doublet_expressions
  458.         self.singlet_expressions = singlet_expressions
  459.         self.wc_doublet_expressions = wc_doublet_expressions
  460.         self.wc_wc_doublet_expressions = wc_wc_doublet_expressions
  461.         self.wc_singlet_expressions = wc_singlet_expressions
  462.         self.game.do_one_scratch()
  463.  
  464.     
  465.     def construct_submission(self, lhs_expressions, rhs_expressions):
  466.         stringValues = self.getAllStringValues()
  467.         self.game.do_one_scratch()
  468.         for idx1 in range(len(lhs_expressions)):
  469.             for idx2 in range(len(rhs_expressions)):
  470.                 if lhs_expressions[idx1][3] == rhs_expressions[idx2][3]:
  471.                     combinedList = []
  472.                     for idx in range(len(lhs_expressions[idx1][2])):
  473.                         combinedList.append(lhs_expressions[idx1][2][idx])
  474.                     
  475.                     combinedList.append('=')
  476.                     for idx in range(len(rhs_expressions[idx2][2])):
  477.                         combinedList.append(rhs_expressions[idx2][2][idx])
  478.                     
  479.                     ok = 1
  480.                     for elem in combinedList:
  481.                         count = combinedList.count(elem)
  482.                         if elem.count('WC:'):
  483.                             pass
  484.                         elif stringValues.count(elem) < count:
  485.                             ok = 0
  486.                         
  487.                     
  488.                     if ok == 1:
  489.                         rlist = self.game.localizer.localize(combinedList)
  490.                         if rlist:
  491.                             return rlist
  492.                         
  493.                     
  494.                 
  495.             
  496.         
  497.         return None
  498.  
  499.     
  500.     def generate_options(self):
  501.         triplet_expressions = self.triplet_expressions
  502.         doublet_expressions = self.doublet_expressions
  503.         singlet_expressions = self.singlet_expressions
  504.         wc_doublet_expressions = self.wc_doublet_expressions
  505.         wc_wc_doublet_expressions = self.wc_wc_doublet_expressions
  506.         wc_singlet_expressions = self.wc_singlet_expressions
  507.         operator_doublets = self.operator_doublets
  508.         operator_singlets = self.operator_singlets
  509.         LEVEL = self.LEVEL
  510.         self.options = []
  511.         tray = self.tray
  512.         spots = self.tray.get_spots()
  513.         self.game.localizer.update_board_map()
  514.         num_commited = self.game.board.get_num_commited()
  515.         if num_commited > 3 and LEVEL > 1:
  516.             rlist = self.construct_submission(triplet_expressions, wc_doublet_expressions)
  517.             if rlist:
  518.                 return rlist
  519.             
  520.             rlist = self.construct_submission(wc_doublet_expressions, triplet_expressions)
  521.             if rlist:
  522.                 return rlist
  523.             
  524.             rlist = self.construct_submission(triplet_expressions, wc_singlet_expressions)
  525.             if rlist:
  526.                 return rlist
  527.             
  528.             rlist = self.construct_submission(wc_singlet_expressions, triplet_expressions)
  529.             if rlist:
  530.                 return rlist
  531.             
  532.         
  533.         if num_commited > 3:
  534.             rlist = self.construct_submission(doublet_expressions, wc_wc_doublet_expressions)
  535.             if rlist:
  536.                 return rlist
  537.             
  538.             rlist = self.construct_submission(wc_wc_doublet_expressions, doublet_expressions)
  539.             if rlist:
  540.                 return rlist
  541.             
  542.             rlist = self.construct_submission(wc_wc_doublet_expressions, singlet_expressions)
  543.             if rlist:
  544.                 return rlist
  545.             
  546.             rlist = self.construct_submission(singlet_expressions, wc_wc_doublet_expressions)
  547.             if rlist:
  548.                 return rlist
  549.             
  550.             rlist = self.construct_submission(wc_doublet_expressions, wc_doublet_expressions)
  551.             if rlist:
  552.                 return rlist
  553.             
  554.             rlist = self.construct_submission(doublet_expressions, wc_doublet_expressions)
  555.             if rlist:
  556.                 return rlist
  557.             
  558.             rlist = self.construct_submission(wc_doublet_expressions, doublet_expressions)
  559.             if rlist:
  560.                 return rlist
  561.             
  562.         
  563.         rlist = self.construct_submission(wc_singlet_expressions, wc_doublet_expressions)
  564.         if rlist:
  565.             return rlist
  566.         
  567.         rlist = self.construct_submission(wc_doublet_expressions, wc_singlet_expressions)
  568.         if rlist:
  569.             return rlist
  570.         
  571.         rlist = self.construct_submission(doublet_expressions, wc_singlet_expressions)
  572.         if rlist:
  573.             return rlist
  574.         
  575.         rlist = self.construct_submission(wc_singlet_expressions, doublet_expressions)
  576.         if rlist:
  577.             return rlist
  578.         
  579.         rlist = self.construct_submission(singlet_expressions, wc_doublet_expressions)
  580.         if rlist:
  581.             return rlist
  582.         
  583.         rlist = self.construct_submission(wc_doublet_expressions, singlet_expressions)
  584.         if rlist:
  585.             return rlist
  586.         
  587.         rlist = self.construct_submission(wc_singlet_expressions, singlet_expressions)
  588.         if rlist:
  589.             return rlist
  590.         
  591.         rlist = self.construct_submission(singlet_expressions, wc_singlet_expressions)
  592.         if rlist:
  593.             return rlist
  594.         else:
  595.             return None
  596.  
  597.  
  598.